package com.tamir.template.bean;

import com.tamir.template.util.StringUtil;

import java.util.List;
import java.util.UUID;

/**
 * @author : tanggh
 * @description : 操作结点
 * @date : 2016/10/26
 */
public class TNode
{
    private String sequenceId;
    boolean isAnalyse = false;
    //结点的逻辑类型
    public TLogicNodeType tLogicType;
    //子结点
    public List<TNode> subNodeList;
    //父结点
    public TNode parentNode;

    public TNode oppositeLogicNode;

    //结点的text
    public String text;

    //表达式
    public String express;

    /**
     * 同位表达式对象,如if 后边跟的是逻辑表达式
     * 这个表达式不能做为子结点,只能做为同位结点
     */
    public TNode expressObject;

    //真实的表式,做过相应字符处理
    public String realExpress;

    public Object outValue;
    public String outKey;

    public Object inputValue;
    public String inputKey;

    //逻辑表达式
    public LogicOperate logicOperate;

    public TNode(){
        this.sequenceId = UUID.randomUUID().toString();
    }

    public String getSequenceId()
    {
        return sequenceId;
    }

    public String getExpress()
    {
        return express;
    }

    public void setExpress(String express)
    {
        this.express = express;
        this.isAnalyse = false;
        this.getRealExpress();
    }

    public TNode getExpressObject()
    {
        return expressObject;
    }

    public void setExpressObject(TNode expressObject)
    {
        this.expressObject = expressObject;
    }

    public LogicOperate getLogicOperate()
    {
        return logicOperate;
    }

    public void setLogicOperate(LogicOperate logicOperate)
    {
        this.logicOperate = logicOperate;
    }

    public TLogicNodeType gettLogicType()
    {
        return tLogicType;
    }

    public void settLogicType(TLogicNodeType tLogicType)
    {
        this.tLogicType = tLogicType;
    }

    public List<TNode> getSubNodeList()
    {
        return subNodeList;
    }

    public void setSubNodeList(List<TNode> subNodeList)
    {
        this.subNodeList = subNodeList;
    }

    public TNode getParentNode()
    {
        return parentNode;
    }

    public void setParentNode(TNode parentNode)
    {
        this.parentNode = parentNode;
    }

    public String getText()
    {
        return text;
    }

    public void setText(String text)
    {
        this.text = text;
    }

    public String getRealExpress()
    {
        if(!isAnalyse){
            this.analyse();
        }
        return realExpress;
    }

    public void setRealExpress(String realExpress)
    {
        this.realExpress = realExpress;
    }

    private void analyse(){
        if(!isAnalyse && StringUtil.isNotEmpty(this.express)){
            String tmpExpress = this.express.replaceAll("\\$\\{", "").replaceAll("\\}", "")
                    .replaceAll("<#", "");
            int index = tmpExpress.lastIndexOf(">");
            /**
             *  如果tmpExpress以">"结尾,去掉">"
             *  表达式中有>号,所以不能直接用replaceAll
             */
            if(index + 1 == tmpExpress.length()){
                tmpExpress = tmpExpress.substring(0, index);
            }
            this.realExpress = tmpExpress;
        }
        this.isAnalyse = true;
    }

    public Object getOutValue()
    {
        return outValue;
    }

    public void setOutValue(Object outValue)
    {
        this.outValue = outValue;
    }

    public Object getInputValue()
    {
        return inputValue;
    }

    public void setInputValue(Object input)
    {
        this.inputValue = input;
    }

    public String getOutKey()
    {
        return outKey;
    }

    public void setOutKey(String outKey)
    {
        this.outKey = outKey;
    }

    public String getInputKey()
    {
        return inputKey;
    }

    public void setInputKey(String inputKey)
    {
        this.inputKey = inputKey;
    }

    public TNode getOppositeLogicNode()
    {
        return oppositeLogicNode;
    }

    public void setOppositeLogicNode(TNode oppositeLogicNode)
    {
        this.oppositeLogicNode = oppositeLogicNode;
    }

    public static void main(String[] args)
    {
        TNode node = new TNode();
        node.setExpress("1+2>0");
        System.out.println(node.getRealExpress());
    }
}
